package com.swjd.demo8.controller;

import com.swjd.demo8.dao.TNewsDao;
import com.swjd.demo8.pojo.TNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class TNewsController {
    @Autowired
    private TNewsDao tNewsDao;
    @RequestMapping("/index")
    public String aaa(Model model){
        List<TNews> tNews = tNewsDao.selectList(null);
        model.addAttribute("tNews",tNews);
        return "index";
    }
    @RequestMapping("/index/{id}")
    public String aaa(Model model,@PathVariable Integer id){
    tNewsDao.deleteById(id);
        return "redirect:/index";
    }
    @RequestMapping("/new")
    public String news(){
        return "news_add";
    }
    @PostMapping("/add")
    public String add(Integer ntid,String ntitle,String nauthor,String nsummary,String ncontent){
        TNews tNews = new TNews();
        tNews.setTopicid(ntid);
        tNews.setTitle(ntitle);
        tNews.setName(nauthor);
        tNews.setSummary(nsummary);
        tNews.setContent(ncontent);
        tNewsDao.insert(tNews);
        return "redirect:/index";
    }
}
